package game.worlds;

import game.utils.DtTools;
import game.data.GameOption;
import hx.geom.Point;

/**
 * 摄像机，用于显示游戏的区域画面
 */
class Camera {
	/**
	 * 世界
	 */
	public var world:World;

	/**
	 * 震动管理器
	 */
	public var shakeManager:ShakeManager = new ShakeManager();

	public function new(world:World) {
		this.world = world;
	}

	/**
	 * 更新摄像机具体显示区域
	 * @param x 坐标X
	 * @param y 坐标Y
	 * @param scale 缩放值
	 */
	public function update(x:Float, y:Float, scale:Float, dt:Float, tween:Float = 1):Void {
		shakeManager.update(dt);
		scale = Math.max(scale, 1.6);
		// 地图边界限制支持
		var stageWidth50 = world.stage.stageWidth / 2 / scale;
		if (x < stageWidth50) {
			x = stageWidth50;
		} else if (x > world.mapWidth - stageWidth50) {
			x = world.mapWidth - stageWidth50;
		}

		// 摄像机居中
		world.cameraBox.x = world.stage.stageWidth / 2;
		world.cameraBox.y = world.stage.stageHeight / 2;
		// 摄像机缩放
		var targetScale = world.cameraBox.scaleX - (world.cameraBox.scaleX - scale) * tween;
		world.cameraBox.scaleX = world.cameraBox.scaleY = targetScale;

		/// 摄像机位移
		var targetX = -x + shakeManager.point.x;
		var targetY = -y + shakeManager.point.y;
		world.worldBox.x -= (world.worldBox.x - targetX) * tween;
		world.worldBox.y -= (world.worldBox.y - targetY) * tween;

		// 更新背景
		var fx = -world.worldBox.x / world.mapWidth;
		var fy = -world.worldBox.y / world.mapHeight;
		for (display in world.bgDisplays) {
			display.update(fx, fy);
		}
	}
}
